package com.fcscs.cservice.client.common;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.fcscs.core.domain.IdNameModel;
import com.fcscs.core.domain.Property;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.cservice.client.common.controller.BookingServiceController;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.client.common.model.restaurants.RestaurantsModelData;
import com.fcscs.cservice.common.CServiceCommonEnums.RestaurantConfigType;
import com.fcscs.cservice.domain.Restaurant;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class CserviceControllerUtils {
	private static CserviceControllerUtils INSTANCE = null;

	private CserviceControllerUtils() {
	}

	public static CserviceControllerUtils getInstance() {
		if (null == INSTANCE) {
			INSTANCE = new CserviceControllerUtils();
		}
		return INSTANCE;
	}

	public void getRestaurantList(final long propId, final ListStore<RestaurantsModelData> restaurantstore) {
		final long orgId = SessionContext.getInstance().getOrgId();
		BookingServiceController.getInstance().getRestaurantList(orgId, propId,
				new AsyncCallbackListener<List<Restaurant>>() {

					@Override
					public void onLogin(String username) {
						BookingServiceController.getInstance().getRestaurantList(orgId, propId, this);
					}

					@Override
					public void onSuccess(List<Restaurant> result) {
						if (result != null) {
							restaurantstore.removeAll();
							
							for (Restaurant rt : result) {
								restaurantstore.add(new RestaurantsModelData(rt));
							}
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
								CoreConstants.CORE_CONSTANTS.system(),
								ShareGWTConstants.SHARE_GWT_CONSTANTS.error()), CoreConstants.CORE_CONSTANTS
								.systemError(), null);
					}
				});
	}
	
	public static List<RestaurantConfigType> getListRestConfigType() {
		List<RestaurantConfigType> list = new ArrayList<RestaurantConfigType>();
		list.add(RestaurantConfigType.EMAIL);
		list.add(RestaurantConfigType.C_CONNECT);
		list.add(RestaurantConfigType.E_CONNECT);
		list.add(RestaurantConfigType.PRINTER);

		return list;
	}
	
	/**
	 * Load property list 
	 * @param propertyStore
	 * @param propertyCombox
	 */
	public static void loadPropertys(ListStore<PropertyIdNameModelData> propertyStore,ComboBox<PropertyIdNameModelData> propertyCombox ) {
		propertyStore.removeAll();
		PropertyIdNameModelData currProMD;
		if(SessionContext.getInstance().getPropertyGroup().size()<1){
			IdNameModel<Property> idName = new IdNameModel<Property>(SessionContext.getInstance().getPropertyId(), SessionContext.getInstance().getPropertyName());
			currProMD = new PropertyIdNameModelData(idName);
			propertyStore.add(currProMD);
		}else{
			int propIndex = 0;
			for(IdNameModel<Property> i : SessionContext.getInstance().getPropertyGroup()){
				propertyStore.add(new PropertyIdNameModelData(i));
				if (propIndex == 0) {
					if (SessionContext.getInstance().getPropertyId() == i.getId()) {
						propIndex = propertyStore.getCount() - 1;
					}
				}
			}
			currProMD = propertyStore.getAt(propIndex);
		}
		propertyCombox.setValue(currProMD);
	}

}
